package com.tiger.userClient.interceptor;


import com.auth0.jwt.interfaces.Claim;
import com.tiger.common.Jwt;
import com.tiger.userClient.http.auth.type.UserRoleType;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Map;
import java.util.Objects;

@Component
public class AuthInterceptor implements HandlerInterceptor{




    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){
        System.out.println("进入拦截器1");

        String patternPath = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        System.out.println(patternPath);

        //Long userId = 0;

        try {

            HttpSession session = request.getSession();

            // 手动获取Environment对象
            Environment environment = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(request.getServletContext())
                    .getEnvironment();

//            if(request.getHeader("abc") != null) {
//                long u = 288;
//                session.setAttribute("userId", u);
//                session.setAttribute("userKey", "31100018");
//                session.setAttribute("role", UserRoleType.Teacher);
//            }

            //String token = request.getHeader(environment.getProperty("AuthTokenName"));
//            long s = 1001048;
//
//            session.setAttribute("userId", s);
//            session.setAttribute("userKey", "gzhcs");
//            session.setAttribute("role", "OTHER");

            boolean isAuth = false;
            String role = "";
            long userId = 0;

            if(session.getAttribute("userId") != null) {
                userId = (Long) session.getAttribute("userId");
            }


            System.out.println("userId");
            System.out.println(userId);


            if(userId > 0) {


                String userKey = (String) session.getAttribute("userKey");
                role = (String) session.getAttribute("role");

                System.out.println("userKey--------");
                System.out.println(userKey);

                isAuth = true;

                if(Objects.equals(userKey, "gzhcs")) {
                    userKey = "31100018";
                }

                request.setAttribute("userKey", userKey);


                if(Objects.equals(role, UserRoleType.Teacher)) {
                    request.setAttribute("isTeacher", true);
                }else if(Objects.equals(role, UserRoleType.Student)) {
                    request.setAttribute("isStudent", true);
                }else {
                    request.setAttribute("isOtherUser", true);
                }
            }


            //isAuth = true;
           // request.setAttribute("userKey", "31100018");

            request.setAttribute("isAuth", isAuth);
            request.setAttribute("userId", userId);
            request.setAttribute("role", role);

            System.out.println("user_id:" + userId);
            System.out.println("role:" + role);

            return true;
        } catch (Exception e) {
            System.out.println("拦截器异常");
            // 处理所有类型的异常的代码块
            System.out.println("发生异常：" + e.getMessage());
        }



        return false;
    }

}
